package com.garen.sys.biz.impl;

import com.garen.sys.dao.IFireOrgDao;
import com.garen.sys.entity.FireOrg;
import com.garen.utils.CommonUtils;
import com.garen.sys.biz.IFireOrgBiz;
import org.springframework.stereotype.Service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.garen.common.*;


@Service
public class FireOrgBizImpl implements IFireOrgBiz {

	@Autowired
	private IFireOrgDao fireOrgDao;
	protected static Log log = LogFactory.getLog(FireOrgBizImpl.class);
	
	/*
	 * 获取指定部门所有子部门
	 * */
	@Override
	public List<Integer> getOrgChildrenID(Integer orgParentId) {
		List<Integer> orgIdList = new ArrayList<>();
		List<FireOrg> orgAllList =  getFireOrgList(new FireOrg());//所有部门
		for(FireOrg org : orgAllList){
			if(isSubOrg(orgAllList, orgParentId, org.getOrgId()))
				orgIdList.add(org.getOrgId());
		}
		return orgIdList;
	}
	
	/*
	*根据部门类型
	*/
	public List<FireOrg> getFireOrgList(int orgType){
		return fireOrgDao.getOrgListByType(orgType);
	}
	
	// orgId1 是否为 orgId2父部门
	@Override
	public boolean isSubOrg(List<FireOrg> orgList, Integer pid, Integer orgId) {
		if (pid == null || orgId == null)return false;
		if (pid.equals(orgId)) return true;
		Integer id = orgId;
		while (true) {
			FireOrg orgTmp = null;
			for (FireOrg org : orgList) {// 查找部门对象
				if (id.equals(org.getOrgId())) {
					orgTmp = org;
					break;
				}
			}
			if (orgTmp == null)
				return false;// 未找到
			if (pid.equals(orgTmp.getOrgParentId()))
				return true;// 找到
			id = orgTmp.getOrgParentId();
		}
	}
		
		
	/*
	*分页查询
	*/
	@Override
	public List<FireOrg> getFireOrgList(PageBean<FireOrg> pb,FireOrg fireOrg){
		return fireOrgDao.getPageList(pb,fireOrg);
	}

	//获取树
	public List<FireOrg> getFireOrgListByTree(MsgBean mbean,FireOrg fireOrg,Integer orgParentId){
		List<FireOrg> orgAllList =  getFireOrgList(new FireOrg());//所有部门
		List<FireOrg> orgDestList = new ArrayList<FireOrg>();
		for(FireOrg org : orgAllList){
			if(isSubOrg(orgAllList, orgParentId, org.getOrgId()))
				orgDestList.add(org);
		}
		mbean.setInfo(orgDestList.size() + "");
		CommonUtils.list2Tree(orgDestList);
		return orgDestList;
	}
	
	@Override
	public List<FireOrg> getFireOrgList(FireOrg fireOrg){
		List<FireOrg> orgList =  fireOrgDao.getList(fireOrg,"org_order");
		Iterator<FireOrg> orgIt = orgList.iterator();
		while(orgIt.hasNext()){
			FireOrg org = orgIt.next();
			if(1 == org.getOrgId()){//删除总队
				orgIt.remove();
				break;
			}
		}
		return orgList;
	}

	@Override
	public List<FireOrg> getFireOrgListByType(FireOrg fireOrg,Integer orgParentId){
		FireOrg org1 = new FireOrg();
		org1.setOrgType(FireOrg.ORG_TYPE_LOCHUS);
		List<FireOrg> orgAllList =  fireOrgDao.getList();//所有部门
		List<FireOrg> orgList =  fireOrgDao.getList(org1,"org_parent_id asc");
		List<FireOrg> orgDestList = new ArrayList<FireOrg>();
		log.debug("parent id = " + orgParentId);
		for(FireOrg org : orgList){
			if(isSubOrg(orgAllList, orgParentId, org.getOrgId()))
				orgDestList.add(org);
		}
		return orgDestList;
	}
	
	/*
	*查询个体
	*/
	@Override
	public FireOrg getFireOrg(FireOrg fireOrg){
		return fireOrgDao.get(fireOrg);
	}

	/*
	*保存
	*/
	@Override
	public int saveFireOrg(MsgBean mbean,FireOrg fireOrg){
		int ret = -1;
		if(fireOrg.getOrgId() == null){
			ret = fireOrgDao.add(fireOrg);
		}else{
			ret = fireOrgDao.update(fireOrg);
		}
		return ret;
	}

	/*
	*删除
	*/
	@Override
	public int delFireOrg(MsgBean mbean,FireOrg fireOrg){
		return fireOrgDao.del(fireOrg);
	}

}
